<?php
namespace think;

class Curler
{
	public static $ip;

	protected static $cookie_arr_firefox = array(
			'Apache' => '707324264905.4368.1481015490264',
			'SINAGLOBAL' => '2802462400721.4795.1480952066916',
			'SUB' => '_2AkMvGQY2f8NhqwJRmP4XyGjiaIRyzg3EieLBAH7sJRMxHRl-yT83qmsOtRAkAkXGlYOnZx76uQ6NtAZOuPlMPw..',
			'SUBP' => '0033WrSXqPxfM72-Ws9jqgMF55529P9D9WhxY--rdbzU.LDu2Pc7wvpq',
			'SWB' => 'usrmdinst_24',
			'ULV' => '1481015490318:3:3:3:707324264905.4368.1481015490264:1480990546207',
			'WBStorage' => '2c466cc84b6dda21|undefined',
			'_s_tentry' => '-',
		);
	protected static $cookie_arr_noone = array(	
			'Apache' => '9213698992144.896.1480857693748',				
			'SCF' => 'AhJaknGQluYdtKzfCDOBriRfDffMLAP6h0DhOnJBFV1oOgRMQzcOIxKXkuYA9vg_lk50IiCt6-DHKl0Q5iyZlLc.',				
			'SINAGLOBAL' =>	'3346921005132.6035.1480748495787',
			'SSOLoginState' => '1480669582',
			'SUB' => '_2AkMvGJdvf8NhqwJRmP4Xzm_lbYhywg_EieLBAH7sJRMxHRl-yT83qkgstRAB3gAz17d1m5HLizrgtDckp8vpQg..',
			'SUBP' => '0033WrSXqPxfM72-Ws9jqgMF55529P9D9W53zwhEKCC1CMoJxmpSc457',
			'SUHB' => '0d4-iXBRLYZPLa',	
			'ULOGIN_IMG' => '14806067783599',			
			'ULV' => '1480857693756:2:2:1:9213698992144.896.1480857693748:1480748495841',
			'UOR' => 'www.lupaworld.com,widget.weibo.com,www.lupaworld.com',
			'WBStorage' => '2c466cc84b6dda21|undefined',
			//'WBtopGlobal_register_version' =>'81b5567db8ceeb18',
			'YF-Page-G0' => '8eed071541083194c6ced89d2e32c24a',
			'YF-Ugrow-G0' =>'b02489d329584fca03ad6347fc915997',				
			'YF-V5-G0' => '590253f9bb559fcb4f19c58020522401',
			//'_T_WM' => '7b9ec4ec0e05a344234753e4dbf40b4c',			
			'_s_tentry' => '-',				
			'login_sid_t' => '90d7b8efc07e08e14f3263ec7ffff4da',				
			//'wvr' => '6',				
			'un' => '13041240048'
		);
	protected static $cookie_arr_login = array(
			'ALF' => '1483665767',		
			'Apache' => '9947170987927.035.1481086179758',				
			'SCF' => 'AhJaknGQluYdtKzfCDOBriRfDffMLAP6h0DhOnJBFV1om25xMdA4I4q6INgl7utc-PA6fgFeTnSuHsUHMdyVfLc.',				
			'SINAGLOBAL' =>	'3346921005132.6035.1480748495787',
			//'SSOLoginState' => '1480669582',
			'SUB' => '_2A251QxQ3DeRxGeNI7lcZ8i3MzDmIHXVWz7x_rDV8PUJbkNANLXnskW2gyNg8Cm7Koo9nTh9Ouo0A0MmCyg..',
			'SUBP' => '0033WrSXqPxfM725Ws9jqgMF55529P9D9W5.CThM4xEPFdNX4naQPVQQ5NHD95QfSo-f1hz0ehMfWs4DqcjZxsLV9cpJeK.4S5tt',
			'SUHB' => '0V5lNSoF7NzR4F',	
			'SWB' => 'usrmdinst_5',
			//'ULOGIN_IMG' => '14806067783599',			
			'ULV' => '1481086179784:8:8:7:9947170987927.035.1481086179758:1481036351749',
			'UOR' => 'www.lupaworld.com,widget.weibo.com,www.baidu.com',
			'WBStorage' => '2c466cc84b6dda21|undefined',
			'WBtopGlobal_register_version' =>'81b5567db8ceeb18',
			//'YF-Page-G0' => '0dccd34751f5184c59dfe559c12ac40a',
			//'YF-Ugrow-G0' =>'ea90f703b7694b74b62d38420b5273df',				
			//'YF-V5-G0' => '4955da6a9f369238c2a1bc4f70789871',
			//'_T_WM' => '5543e85dfb626ec14ab19f6b2888897e',			
			'_s_tentry' => 'www.baidu.com',				
			//'login_sid_t' => 'db355e7c77a18eeef77a0487006f86d9',				
			'wvr' => '6',				
			//'un' => '13041240048'
		);
	protected static $cookie_arr_anyone = array(
			'Apache' 	 => '8745769080871.917.1480426098121',
			'SCF'	 	 => 'AhbE7yHYfTie-0czWrSUh4Qpo3h0CZpPxkYFCv1DgpkHxTIfBkPvynV8iB3xygAX6-AiKIQb6OdB_KqBI0x4Ze4.',
			'SINAGLOBAL' =>	'9989965864081.162.1479261799065',
			'SUB'		 => '_2AkMvYSLjdcNhrAZXnf0dzmjia4hH-jzEiebBAn7uJhMyAxgv7mYSqSUvVuK7ccTndJr0GZc1bXH2FvjC2A..',
			'SUBP'		 =>	'0033WrSXqPxfM72wWs9jqgMF55529P9D9W5.CThM4xEPFdNX4naQPVQQ5JpVF020ehBpeoB7ehBR',
			'SUHB'       => '0FQrUMnUtklhWi',
			'SWB'        => 'usrmdinst_15',
			'ULOGIN_IMG'	 => '14804371664949',
			'ULV'        => '1480426098374:11:11:4:8745769080871.917.1480426098121:1480388194936',
			'UOR'	     => 'www.lupaworld.com,widget.weibo.com,login.sina.com.cn',
			'WBStorage'  => '2c466cc84b6dda21|undefined',
			//'YF-Page-G0' => '0acee381afd48776ab7a56bd67c2e7ac',
			//'YF-Ugrow-G0'=> '1eba44dbebf62c27ae66e16d40e02964',
			//'YF-V5-G0'   => '4d1671d4e87ac99c27d9ffb0ccd1578f',
			'_T_WM'      => '7b9ec4ec0e05a344234753e4dbf40b4c',
			'_s_tentry'  => 'login.sina.com.cn',
			'login_sid_t'=>	'bac2b33667fa0c6b8ff57dff9a542fed',
			'un'		 =>	'13041240048'
		);
	private static function genCookie() {
		//self::$cookie_arr_login_one['ALF'] = time();
		$cookie = '';
		foreach (self::$cookie_arr_login as $key => $value) {
			if($key != 'wvr')
				$cookie .= $key . '=' . $value . ';';
			else
				$cookie .= $key . '=' . $value;
		}

		return $cookie;
	}

	public static function request($method, $url, $fields=array(), $flag = 0)
	{
		$user_agent_firefox = 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:50.0) Gecko/20100101 Firefox/50.0';
		$user_agent_chrome = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.75 Safari/537.36';
		if ($flag == 1 || self::$ip == '')
		{
			$rand_ip = self::get_rand_ip();
			self::$ip = $rand_ip;	
		}
		else 
		{
			$rand_ip = self::$ip;
		}
		$header = array( 
			"CLIENT-IP:$rand_ip", 
			"X-FORWARDED-FOR:$rand_ip", 
		); 
		$ch = curl_init($url);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_COOKIE, self::genCookie());
        curl_setopt($ch, CURLOPT_USERAGENT, $user_agent_chrome);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, 10);
        curl_setopt($ch, CURLOPT_REFERER, "http://www.163.com/"); //构造来路
        curl_setopt($ch, CURLOPT_HEADER, 1);
        if ($method === 'POST')
        {
            curl_setopt($ch, CURLOPT_POST, true );
            curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
        }
        $result = curl_exec($ch);
        return $result;
	}
	public static function get_rand_ip()
	{
  		$arr_1 = array("218","218","66","66","218","218","60","60","202","204","66","66","66","59","61","60","222","221","66","59","60","60","66","218","218","62","63","64","66","66","122","211");
  		$randarr= mt_rand(0,count($arr_1)-1);
	  	$ip1id = $arr_1[$randarr];
	  	$ip2id=  round(rand(600000,  2550000)  /  10000);
	  	$ip3id=  round(rand(600000,  2550000)  /  10000);
	  	$ip4id=  round(rand(600000,  2550000)  /  10000);
	  	return  $ip1id . "." . $ip2id . "." . $ip3id . "." . $ip4id;
	} 
}